
const { appealList } = require('../../../../api/index');
const { filePreview } = require('../../../../api/index');
const { isFinished } = require('../../../../utils/util');
const { getBatchDict } = require('../../../../utils/dict');
const { fb } = require('../../../utils/base64/fb');

// pages/home/index.js
Page({
  /*** 页面的初始数据 */
  data: {
    fb,
    page: {
      pageNumber: 1,
      pageSize: 20
    },
    total: 0, //  总条数
    finished: false,
    loading: false,
    dataList: [],
    list: [],
    applyTypeDicts: [],
    applyProcessStateDicts: []
  },
  onRefresh() { //  下拉刷新
    console.log('oooo');
    this.data.page.pageNumber = 1;
    this.data.finished = false;
    //  清空原来的数组，避免缓存造成的误差
    this.setData({ list: [] }, () => {
      fetchAppealList.call(this);
    })
  },
  onClick( event ) {
    var page = getCurrentPages();
    //  保存当前的位置，便于后期跳转回来的时候进行其他操作
    const curIndex = page.length - 1;

    const { url } = event.currentTarget.dataset;
    wx.navigateTo({ 
      url,
      success(e) {  //  每次进入 tab页都刷新一下
          const toPage = page[ curIndex ];
          if (toPage == undefined || toPage == null) return;
          wx.setStorageSync('success_back_index', curIndex);
        }
    });
  },
  onJump(event) {
    const { url } = event.currentTarget.dataset;
    wx.navigateTo({ url });
  },
  loadMore() {  //  加载更多
    fetchAppealList.call(this);
  },
  /*** 生命周期函数--监听页面加载 */
  async onLoad(options) {
    //  加载 字典项
    await loadDict.call(this);
    //  加载数据
    fetchAppealList.call(this);
  },
})

//  获取 热门活动列表数据
async function fetchAppealList() {
  wx.showLoading({ title: '加载中...' });
  const { errors, result, totalCount } = await appealList( this.data.page );
  if(errors.lenght > 0) {
    return;
  }

  this.data.total = totalCount;
  //  判断数据是不是都已经加载完了
  this.setData({ finished: isFinished(totalCount, this.data.page.pageNumber * this.data.page.pageSize ) }); 
  //  加载更多数据
  this.setData({ list: this.data.list.concat( setResult2.call( this, result )) });
  this.data.page.pageNumber ++;

  wx.hideLoading();
} 

//  处理数据
function setResult( data = [] ) {
  const _data = JSON.parse(JSON.stringify(data));

  _data.map(item => {
    item.carouselPhoto = filePreview( '/file/preview/' + item.carouselPhoto );

    return item;
  })

  return _data;
}

//  处理数据
function setResult2( data = [] ) {
  const _data = JSON.parse(JSON.stringify(data));

  _data.map(item => {
    item.activityPhoto = filePreview( '/file/preview/' + item.activityPhoto );
    console.log( this.data.applyTypeDicts);
    item.statusClass = 'status-' + item.processState;
    item.appealType = this.data.applyTypeDicts.find(_s => _s.code === item.appealType)?.name;
    item.processState = this.data.applyProcessStateDicts.find(_s => _s.code === item.processState )?.name;
    return item;
  })

  return _data;
}


//  加载页面所需要的 - 
async function loadDict() {
  const [ applyTypeDicts, applyProcessStateDicts ] = await getBatchDict(['APPEAL_TYPE', 'APPEAL_PROCESS_STATE']);
  this.data.applyTypeDicts = applyTypeDicts;
  this.data.applyProcessStateDicts = applyProcessStateDicts;
}